using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Collections;


namespace Stopwatch
{
    public partial class ViewReport : Form
    {
        private RunReport runReport;
        private string FullPath;
        private string ReportNumber;
        private int SelectedIndex;

        public ViewReport()
        {
            InitializeComponent();
        }

        private void ViewReport_Load(object sender, EventArgs e)
        {
            //List view column header
            ColumnHeader chReportNumber = new ColumnHeader();
            ColumnHeader chDateTime = new ColumnHeader();
            ColumnHeader chAverage = new ColumnHeader();

            chReportNumber.Text = "Report";
            chDateTime.Text = "Date & Time";
            chAverage.Text = "Average";

            lsViewReport.Columns.Add(chReportNumber);
            lsViewReport.Columns.Add(chDateTime);
            lsViewReport.Columns.Add(chAverage);

            runReport = new RunReport();

            LoadListView();
            
        }

        private void LoadListView()
        {
            DirectoryInfo dirInfo = new DirectoryInfo(Common.PATH_REPORTS);
            IComparer IDateComparer = new DateComparerDes();

            try
            {
                FileInfo[] fileInfoArray = dirInfo.GetFiles("*.xml");
                
                Array.Sort(fileInfoArray, IDateComparer);

                string sAverage = string.Empty;
                string sFastest = string.Empty;
                string sSlowest = string.Empty;
                string sLapCount = string.Empty;
                string sRepNumber = string.Empty;
                string sListItem = string.Empty;
                
                if(lsViewReport.Items.Count > 0)
                    lsViewReport.Clear();

                foreach (FileInfo fileInfo in fileInfoArray)
                {
                    sListItem = fileInfo.CreationTime.ToString("MM/dd/yy   ");
                    sListItem += fileInfo.CreationTime.ToShortTimeString();
                    
                    runReport.GetSummaryDataFromXML(fileInfo.FullName, ref sAverage, ref sFastest, ref sSlowest, ref sLapCount, ref sRepNumber);
                    
                    lsViewReport.Items.Add(new ListViewItem(new string[] {sRepNumber, sListItem, sAverage, fileInfo.FullName }));

                    if (lsViewReport.Items.Count == 1)
                    {
                        lbFastest.Text = String.Format("Fastest:\r\n{0}", sFastest);
                        lbSlowest.Text = String.Format("Slowest:\r\n{0}", sSlowest);
                        lbLapCount.Text = String.Format("Lap Count:\r\n{0}", sLapCount);
                    }
                }

                if (lsViewReport.Items.Count > 0)
                {
                    lsViewReport.Items[0].Selected = true;
                    lsViewReport.Items[0].Focused = true;
                }

                lsViewReport.Refresh();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(String.Format("Stopwatch LoadListView Exception Msg: {0} Stack Info: {1}",
                    ex.Message, ex.StackTrace));
            }
        }


        private void mniExit_Click(object sender, System.EventArgs e)
        {
            CloseViewReport();
        }

        private void mniDone_Click(object sender, System.EventArgs e)
        {
            CloseViewReport();
        }

        private void CloseViewReport()
        {
            //Free resources and exit form
            this.Close();
        }

        private void ShowLapSummary()
        {
            Cursor.Current = Cursors.WaitCursor;
            ViewLapSummary viewSummary = new ViewLapSummary(FullPath);
            viewSummary.Show();
            Cursor.Current = Cursors.Default;
        }

        void mniSummary_Click(object sender, System.EventArgs e)
        {
            ShowLapSummary();
        }

        void mniDelete_Click(object sender, System.EventArgs e)
        {
            string sMessage = String.Format("Are you sure you want to delete Summary Report {0}?", ReportNumber);

            DialogResult msgBoxRes = MessageBox.Show(sMessage, "Stopwatch", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);

            if (msgBoxRes == DialogResult.Yes)
            { 
                //Delete selected report
                try
                {
                    //Delete xml report
                    runReport.DeleteXMLReport(FullPath);

                    if (this.lsViewReport.Items.Count > 1)
                    {
                        //remove deleted item from listview
                        this.lsViewReport.Items.RemoveAt(SelectedIndex);

                        //Select the first item from listview
                        this.lsViewReport.Items[0].Selected = true;

                        this.lsViewReport.Items[0].Focused = true;
                    }
                    else 
                    {
                        ReturnToMainForm();
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(String.Format("Stopwatch mniDelete_Click Exception Msg: {0} Stack Info: {1}",
                        ex.Message, ex.StackTrace));
                }
            }
        }

        private void ReturnToMainForm()
        {
            //Call main form
            Form1 frm1 = new Form1();

            frm1.Show();

            //Close form, it does not have more items to show
            this.Close();
        }

        private void mniDeleteAll_Click(object sender, System.EventArgs e)
        {
            string sMessage = "Are you sure you want to delete ALL Summary Reports?";

            DialogResult msgBoxRes = MessageBox.Show(sMessage, "Stopwatch", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);


            if (msgBoxRes == DialogResult.Yes)
            {
                try
                {
                    runReport.DeleteAllXMLReport();
                    ReturnToMainForm();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(String.Format("Stopwatch lsViewReport_SelectedIndexChanged Exception Msg: {0} Stack Info: {1}",
                        ex.Message, ex.StackTrace));
                }
            }
        }


        private void lsViewReport_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string sFastest = string.Empty;
                string sSlowest = string.Empty;
                string sLapCount = string.Empty;
                string sAverage = string.Empty;

                ListView.SelectedIndexCollection selectIndexColl = ((ListView)sender).SelectedIndices;

                foreach (int index in selectIndexColl)
                {
                    SelectedIndex = index;
                    FullPath = ((ListView)sender).Items[index].SubItems[3].Text;
                }

                if (FullPath == string.Empty)
                {
                    return;
                }
                else
                {
                    runReport.GetSummaryDataFromXML(FullPath, ref sAverage, ref sFastest, ref sSlowest, ref sLapCount, ref ReportNumber);

                    lbFastest.Text = String.Format("Fastest:\r\n{0}", sFastest);
                    lbSlowest.Text = String.Format("Slowest:\r\n{0}", sSlowest);
                    lbLapCount.Text = String.Format("Lap Count:\r\n{0}", sLapCount);
                }
            }
            catch (Exception ex)
            {
                
                Debug.WriteLine(String.Format("Stopwatch lsViewReport_SelectedIndexChanged Exception Msg: {0} Stack Info: {1}",
                    ex.Message, ex.StackTrace));
            }
        }

        private void lsViewReport_KeyDown(object sender, KeyEventArgs e)
        {
            if ((Keys)e.KeyValue == Keys.Return || (Keys)e.KeyValue == Keys.Enter)
            {
                ShowLapSummary();
                e.Handled = true;
            }
        }

   }
}